#include "shared.h"
#include "init.h"
#include "timers.h"
#include "usart.h"

void init()
{
	ADCON1 = 0b00001111; // all digital
	PORTB = 0;
	TRISB = 0;
	TRISC = 0xd4;	// RC0 = RST, RC1 = CS3, RC3 = LED, RC6 = tx
	PORTC = 1;
	PORTD = 0;
	TRISD = 0;
	CCP2CON = 0b1011; // compare mode
	CCPR2 = 62500; // 50 ms
	PIE2bits.CCP2IE = 1;
	PIR2bits.CCP2IF = 0;
	OpenTimer3(TIMER_INT_OFF & T3_16BIT_RW & T3_PS_1_8 & 
			T1_CCP1_T3_CCP2 & T3_SOURCE_INT);
// the library lies about its accuracy; the following is necessary:
	T3CONbits.T3CCP1 = 1;
	
	T0CON = 0b00000111; // off, 16-bit, internal clock, 256x prescale

	OpenUSART(USART_RX_INT_ON & USART_TX_INT_OFF & USART_ASYNCH_MODE
			& USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 21); 
		// 115.2 kBaud
	RCONbits.IPEN = 0; // no prioritized interrupts
	INTCONbits.PEIE = 1;
	INTCONbits.GIE = 1; // enable interrupts
}

